import h5py
import numpy as np
from sklearn.neighbors import KernelDensity
from scipy.stats import invgamma

with h5py.File("timing_samples.h5", 'r') as file_in:
    hit = file_in["hit"][()]
    nonhit = file_in["nonhit"][()]

x=hit["PulseTime"]

kde = KernelDensity().fit(x[:,np.newaxis])
Xrange = np.linspace(x.min(), x.max(), 100)
log_dens = kde.score_samples(Xrange[:,np.newaxis])

import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(Xrange, np.exp(log_dens), 'r', label='kde')
a=4; loc=6; scale=120
ax.plot(Xrange, invgamma.pdf(Xrange, a=a, loc=loc, scale=scale), 'k-', label=f'IG({a},{scale},{loc})')
ax.legend()
ax.set_ylabel('pdf')
ax.set_ylim((0, 0.03))
ax2=ax.twinx()
ax2.hist(x, bins=1000, alpha = 0.5)
ax2.set_ylabel('hist')
ax.set_title('Kernel density estimate')
plt.show()
 
